package com.xoa.service.party;


import com.xoa.dao.party.CommunityLxdwxxcjMapper;
import com.xoa.model.party.CommunityLxdwxxcj;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Transient;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommunityLxdwxxcjService {

    @Autowired
    private CommunityLxdwxxcjMapper communityLxdwxxcjMapper;

    /**
     * 查询
     * @return
     */
      public ToJson select(HttpServletRequest request, CommunityLxdwxxcj communityLxdwxxcj,
                           Integer page, Integer pageSize, boolean useFlag, String export, HttpServletResponse response){
              ToJson json=new ToJson();
                 Map<String,Object> map=new HashMap<String,Object>();
                 PageParams params = new PageParams();
                  try{
//                      map.put("lxdwxxcjId",communityLxdwxxcj.getLxdwxxcjId());
//                      map.put("companyName",communityLxdwxxcj.getCompanyName());
//                      map.put("page",page);
//                      map.put("pageSize",pageSize);
                      params.setPage(page);
                      params.setPageSize(pageSize);
                      map.put("communityLxdwxxcj",communityLxdwxxcj);
                      map.put("params",params);
                      List<CommunityLxdwxxcj> lxdwxxcjs=communityLxdwxxcjMapper.Commnuity(map);
                      if(export==null){
                          export="0";
                      }
                      if (export.equals("1")){
                          if (lxdwxxcjs !=null && lxdwxxcjs.size()>0) {
                              for (CommunityLxdwxxcj list : lxdwxxcjs){
                                  if (list.getLeix()!= null){
                                      if (1==list.getLeix()){
                                          list.setType("小歌厅");
                                      }else if (2==list.getLeix()){
                                          list.setType("小餐饮");
                                      }else if (3==list.getLeix()){
                                          list.setType("小网吧");
                                      }else if (4==list.getLeix()){
                                          list.setType("小洗浴");
                                      }else if (5==list.getLeix()){
                                          list.setType("小旅馆");
                                      }else if (6==list.getLeix()){
                                          list.setType("小酒吧");
                                      }else if (7==list.getLeix()){
                                          list.setType("其他");
                                      }else {
                                          list.setType("");
                                      }
                                  }
                              }
                          }
                          HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("六小单位信息采集", 9);
                          String[] secondTitles = {"单位名称", "负责人电话","负责人", "类型", "地址"};
                          HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                          String[] beanProperty = {"companyName", "personTel","person", "type", "addresss"};
                          HSSFWorkbook workbook = null;
                          workbook = ExcelUtil.exportExcelData(workbook2, lxdwxxcjs, beanProperty);
                          OutputStream out = response.getOutputStream();
                          String filename = "六小单位信息采集信息记录表.xls";
                          filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                          response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                          response.setHeader("content-disposition", "attachment;filename=" + filename);
                          workbook.write(out);
                          out.close();
                      }
                    //  int count=communityLxdwxxcjMapper.countCommnuity(communityLxdwxxcj.getCompanyName());
                      json.setObj(lxdwxxcjs);
                      json.setTotleNum(params.getTotal());
                      json.setMsg("ok");
                      json.setFlag(0);
                      return json;
                  }catch (Exception e){
                      json.setMsg("ok");
                      json.setFlag(0);
                      e.printStackTrace();
                  }
              return json;
      }

    /**
     * 修改
     * @return
     */
    @Transient
    public ToJson update(HttpServletRequest request, CommunityLxdwxxcj communityLxdwxxcj){
        ToJson json=new ToJson();
            try{
                int count=communityLxdwxxcjMapper.updateByPrimaryKey(communityLxdwxxcj);
                if(count>0){
                    json.setFlag(0);
                    json.setMsg("ok");
                    return json;
                }
                json.setFlag(0);
                json.setMsg("修改失败");
            }catch (Exception e){
                e.printStackTrace();
            }
        return json;
    }

    /**
     * 删除
     * @return
     */
    @Transient
    public ToJson delete(Integer lxdwxxcjId){
        ToJson json=new ToJson();
        try{
            int count=communityLxdwxxcjMapper.deleteByPrimaryKey(lxdwxxcjId);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("删除失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 添加
     * @return
     */
    @Transient
    public ToJson add(HttpServletRequest request, CommunityLxdwxxcj communityLxdwxxcj){
        ToJson json=new ToJson();
        try{
            int count=communityLxdwxxcjMapper.insert(communityLxdwxxcj);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("添加失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
}
